<template>
	<view class="login-container">
		<uni-icons class="login-head" type="contact-filled" size="100"></uni-icons>
		
		<button type="primary" class="login-btn" @click="getUserInfo">一键登录</button>
		<view class="login-tip">登录后尽享更多权益</view>
	</view>
</template>

<script>
	import {mapMutations, mapState} from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		computed: {
			...mapState('my-user', ['redirectInfo'])
		},
		methods: {
			...mapMutations('my-user', ['updateUserInfo', 'updateToken', 'updateRedirectInfo']),
			getUserInfo() {
				uni.getUserProfile({
					desc: '用户登录',
					lang: 'zh_CN',
					success: (res) => {
						this.updateUserInfo(res.userInfo);
						console.log(res);
						this.getToken(res)
					},
					fail: (err) => {
						
						return uni.$showMsg('您取消了登录授权！')
					}
				})
			},
			async getToken(info) {
				const [err, res] = await uni.login().catch(err => err)
				if (res.errMsg !== 'login:ok') return uni.$showMsg('登录失败！')
				
				const query = {
					code: res.code,
					encryptedData: info.encryptedData,
					iv: info.iv,
					rawData: info.rawData,
					signature: info.signature
				}
				
				const {data: loginResult} = await uni.$http.post('/api/public/v1/users/wxlogin', query)
				console.log(loginResult);
				// if (loginResult.meta.status !== 200) return uni.$showMsg('登录失败！')
				
				this.updateToken('123')
				
				this.navigateBack()
				
				return uni.$showMsg('登录成功！')
			},
			navigateBack() {
				if (this.redirectInfo && this.redirectInfo.openType === 'switchTab') {
					uni.switchTab({
						url: this.redirectInfo.from,
						complete: () => {
							this.updateRedirectInfo(null)
						}
					})
					
					
				}
			}
		}
		
	}
</script>

<style lang="scss">
	page {
		background-color: white;
	}
	.login-container {
		height: 750rpx;
		background-color: #F8F8F8;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative;
		overflow: hidden;
		&::after {
			content: ' ';
			width: 100%;
			height: 40px;
			background: white;
			position: absolute;
			bottom: 0;
			left: 0;
			border-radius: 100%;
			transform: translateY(50%);
		}
		.login-head {
			width: 100px;
			height: 150px;
		}
		.login-btn {
			width: 90%;
			border-radius: 100px;
			background-color: #c00000;
		}
		.login-tip {
			color: gray;
			margin-top: 15px;
		}
	}
</style>